package com.syni.mdd.sjb.common.component.utils.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @className VideoUtils
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/26 15:34
 * @VERSION 1.0
 **/
public class CloudVideoUtils {
    private static Logger logger = LoggerFactory.getLogger(CloudVideoUtils.class);

    public static String getSignUrl(String videoUrl){
        String[] urlSplit = videoUrl.split("/");
        List<String> apiUrls = new ArrayList<>();
        for(int ii = 3;ii < urlSplit.length - 1;ii++){
            apiUrls.add(urlSplit[ii]);
        }
        String apiUrl = "/" + StringUtils.join(apiUrls,"/") + "/";
        String apiKey = "TsGV0WABYa9Hbns0r4ar";
        //当前时间搓
        Integer timestamp = (int)(System.currentTimeMillis() / 1000) + 3600 * 24;
        String t = String.format("%08X", timestamp).toLowerCase();
        //随机字符串
        String randomStr = CDNUtils.getStringRandom(8);
        //接口url
        //String apiUrl = "/c0f0a894vodtranscq1254456672/53e2b2575285890791338292395/";
        //生成md5sign
        String sign = WebUtils.MD5(apiKey + apiUrl + t + randomStr).toLowerCase();
        System.out.println(sign);
        System.out.println(sign.length());
        System.out.println("rs :" + randomStr);
        System.out.println("t : " + t);
        System.out.println("sign: " + sign);
        String signUrl = String.format("%s?t=%s&us=%s&sign=%s",videoUrl,t,randomStr,sign);
        return signUrl;
    }
}
